package cn.topcodes.tcsf.admin.mapper;

import cn.topcodes.tcsf.admin.service.security.Resource;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface SecurityMapper {

	/*通过用户ID查找所有关联角色代码*/
	@Select("select role.code from t_sys_role role join t_sys_user_role_rela rela on role.id = rela.role_id where rela.user_id = #{userId}")
	List<String> getRoleCodeByUserId(long userId);

	/*通过用户ID查找所有关联权限代码*/
	@Select("select distinct perm.code from t_sys_permission perm join t_sys_role_permission_rela rela on perm.id = rela.perm_id " +
			"where rela.role_id in (select role.id from t_sys_role role join t_sys_user_role_rela rela on role.id = rela.role_id where rela.user_id = #{userId})")
	List<String> getPermCodeByUserId(long userId);

	@Select("select url.url as url, perm.code as code from t_sys_permission perm join t_sys_url_permission_rela rela on perm.id = rela.perm_id join t_sys_url url on rela.url_id = url.id"
			+ " UNION "
			+ "select menu.url as url, perm.code as code from t_sys_permission perm join t_sys_menu_permission_rela rela on perm.id = rela.perm_id join t_sys_menu menu on rela.menu_id = menu.id;")
	List<Resource> findPermDef();

}
